package com.laozhao.test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;



public class SwingWorkerExample  extends JFrame implements ActionListener{
    
	private JButton buttonBack = new JButton("buttonBack");
	private JButton buttonEDT = new JButton("buttonEDT");
	private JLabel label = new JLabel("运行线程 :");
	
    public SwingWorkerExample() {
		super("后台线程演示系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initComponents();
    }
    
    private void initComponents() {
    	JPanel pan = new JPanel();
    	pan.setLayout(new BorderLayout());
    	pan.add(label,BorderLayout.CENTER);

    	JPanel buttonPan = new JPanel();
    	buttonPan.setLayout(new FlowLayout());
    	buttonPan.add(buttonBack,BorderLayout.SOUTH);
    	buttonPan.add(buttonEDT,BorderLayout.SOUTH);
    	
    	buttonBack.setToolTipText("此按钮被按下，将在后台运行一个线程，大概12秒后线程完成，在label上显示 【* 后台运行完毕！ *】 字样 ！,这段时间内程序可以相应其他事件");
    	buttonEDT.setToolTipText("此按钮被按下，将在后台运行一个线程，大概12秒后线程完成，在label上显示 【 & 前台运行完毕 ！& 】 字样 ！,这段时间内程序无响应");
    	
    	pan.add(buttonPan,BorderLayout.SOUTH);
    	
    	
    	this.getContentPane().add(pan);
    	this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	this.setBounds(120, 100, 500, 200);
		
		buttonBack.addActionListener(this);
		buttonEDT.addActionListener(this);
	}

	public static void main(String[] s){
		SwingWorkerExample sw = new SwingWorkerExample();
		sw.setVisible(true);
    }

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonBack){
			System.out.println("^^^^^ the buttonBack is checked !");
			new SwingWorker<String, Void>() {   
		        @Override  
		        protected String doInBackground() { 
					System.out.println("^^^^^ the doInBackground is star !");  
		         return readHugeData();   
		        } 
				@Override  
		        protected void done() {   
		            try {
						System.out.println("^^^^^ the doInBackground is done !");  
		            	printLanel( get().toString());
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}   
		        }   
		    }.execute();   
		}else if (e.getSource() == buttonEDT){
			try {
				printLanel(" *  前台运行开始！ * ");
				long x = new  Long(0);
				while(x < 5){
					Thread.sleep(250);
					x ++ ;
				}
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			printLanel(" & 前台运行完毕 ！& ");
		}
	}
	  
    private String readHugeData() {
    	Long x = new Long(0);
    	printLanel(" * 后台运行开始！ * ");
    	while(x < 50){
    		try {
				System.out.println("^^^^^ the doInBackground is working………………………………………………………… !");  
				Thread.sleep(250);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		x++;
    	}
		System.out.println("^^^^^ the doInBackground is end !");  
		return " * 后台运行完毕！ * "; 
//		return 0;
	}
    
    private synchronized void printLanel(String t){
		System.out.println("^^^^^ the synchronized label is rePrinted by :" + t);  
    	label.setText(label.getText() + t);
    }
    
    private class aSwingWorker extends SwingWorker{

		@Override
		protected Object doInBackground() throws Exception {
			// TODO Auto-generated method stub
			return null;
		}
    	
    }
}
